#自定义工具类
from celery import current_app
from flask import session, g
import functools

from info.models import User


def do_index_class(index):
    '''返回指定索引'''
    if index == 0:
        return  "first"
    elif index == 1:
        return  "second"
    elif index == 2:
        return "third"
    return ""

def user_login_data(news_detail):
    # 使用functools函数装饰内层函数可以保持当前装饰器的__name__值不变
    @functools.wraps(news_detail)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id", None)
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user =user
        return news_detail(*args, **kwargs)

    return  wrapper


# def query_user_data():
#     user_id = session.get("user_id", None)
#     user = None
#     if user_id:
#         try:
#             user = User.query.get(user_id)
#         except Exception as e:
#             current_app.logger.error(e)
#         return user
#     return None

